package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
/**
 * http://bbs.pchome.net/  爱玩论坛
 * @author Persh
 */
public class PchomeBbsAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://bbs.pchome.net/thread-[0-9]*-1-1.html",
				"http://.*.pchome.net/thread_[0-9]*_[0-9]*_[0-9]*__.html",
				"http://.*.pchome.net/thread_[0-9]*_[0-9]*_[0-9]*___.html",
				"http://.*.pchome.net/thread_[0-9]*_[0-9]*_[0-9]*.html",
				"http://dcclub.pchome.net/thread-[0-9]*-1-1.html",
				"http://sjbbs.pchome.net/thread-[0-9]*-1-1.html"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		bbs.setUrl(url);


		String title = null;
		String content = null;
		String author = null;
		Long date = null;
		Integer clickNum=null;
		Integer commentNum=null;

		Document doc = Jsoup.parse(htmltxt);

		title = doc.select("title").text();
		title = StringUtils.substringBefore(title, "_");

		Element firstDiv = doc.select("div#contentList > table[class=bbs_post]").first();
		if(firstDiv!=null){
			date = DateUtils.matchDate(firstDiv.select("div.post_date").text());
			content = HtmlCleaner.getContentHtml(url,  firstDiv.select("div.edit_mcont"));
			author = firstDiv.select("dl.user_head").text();
		}

		if(url.startsWith("http://club.pchome.net/")){
			title = doc.select("h1").text();
			title = StringUtils.substringAfter(title, "主题:");

			firstDiv = doc.select("div#detail-content > div[class=item]").first();
			if(firstDiv!=null){
				date = DateUtils.matchDate(firstDiv.select("div.p_time").text());
				content = HtmlCleaner.getContentHtml(url,  firstDiv.select("div.mc"));
				author = firstDiv.select("div.author").text();
			}
			String text = doc.select("div#topic-info").text();
			text = StringUtils.substringBetween(text, "[加入收藏]", "已");
			clickNum = NumberUtils.matchNumber(StringUtils.substringBefore(text, "回复"));
			commentNum = NumberUtils.matchNumber(StringUtils.substringAfter(text, "回复"));
			bbs.setUpdateUrl(url);
		}

		bbs.setClickNum(clickNum);
		bbs.setCommentNum(commentNum);
		bbs.setTitle(StringUtils.trimSpace(title));
		bbs.setContent(content);
		bbs.setAuthor(author);
		bbs.setDate(date);
		return bbs;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(updateUrl!=null && updateUrl.startsWith("http://club.pchome.net/")){
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if(responseToURL!=null){
					String htmltxt = responseToURL.getHtml();
					Document doc = Jsoup.parse(htmltxt);
					Integer clickNum=null;
					Integer commentNum=null;
					String text = doc.select("div#topic-info").text();
					text = StringUtils.substringBetween(text, "[加入收藏]", "已");
					clickNum = NumberUtils.matchNumber(StringUtils.substringBefore(text, "回复"));
					commentNum = NumberUtils.matchNumber(StringUtils.substringAfter(text, "回复"));
					meta.setClickNum(clickNum);
					meta.setCommentNum(commentNum);
					return meta;
				}
			}
		}
		return null;
	}
    
	
}
